// pages/activitys/detail/detail.js
var WxParse = require('../../../wxParse/wxParse.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityId: 0,
    info: {},
    isFavFlag: false
  },

  /**
   * 转向报名页面
   * @param {*} e 
   */
  join(e) {
    if(this.data.isWxLogin){
      wx.navigateTo({
        url: '../join/join?id=' + e.currentTarget.dataset.id
      });
    }else{
      wx.redirectTo({
        url: '/pages/index/index?tab=my'
      });
    }
    
  },

  /**
   * 收藏活动
   */
  fav(e) {
    let _that = this;
    if(_that.data.isWxLogin){
      let _url = app.globalData.apiServerUrl + '/fav';
      let _isFavFlag = true;
      if(_that.data.isFavFlag){
        _url += '/remove';
        _isFavFlag = false;
      }
      wx.request({
        url: _url,
        method: 'POST',
        data: {
          userId: _that.data.userInfo.userId,
          activityId: this.data.activityId
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
        },
        success(res) {
          var _d = res.data;
          if(_d.status && _d.code == 200) {
            _that.setData({
              isFavFlag: _isFavFlag
            });
          }
        }
      });
    }else{
      wx.redirectTo({
        url: '/pages/index/index?tab=my'
      });
    }







  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      activityId: options.id
    });
    this.getActivityInfo();
    this.getCooikeUser();
    this.isFav();
  },

  /**
   * 获取活动详细信息
   */
  getActivityInfo() {
    let _that = this;
    let _url = app.globalData.apiServerUrl + '/detail/' + this.data.activityId;
    wx.request({
      url: _url,
      success(res) {
        var _d = res.data;
        if(_d.code == 200 && _d.status){
          WxParse.wxParse('article' , 'html', _d.data.content, _that, 5);
          _that.setData({
            info: _d.data
          });
        }
      }
    });

  },


  getCooikeUser() {
    let _isLogin = wx.getStorageSync('isWxLogin');
    if(!_isLogin){//TOOD 提示用户登录
      // wx.redirectTo({
      //   url: '/pages/index/index'
      // });
      this.setData({
        isWxLogin: false
      });
    }else{
      let _loginU = wx.getStorageSync('loginUserInfo');
      this.setData({
        userInfo: _loginU,
        isWxLogin: true
      });
    }
  },

  /**
   * 判断是否收藏当前活动
   */
  isFav() {
    let _that = this;
    let _url = app.globalData.apiServerUrl + '/fav/check';
    wx.request({
      url: _url,
      method: 'POST',
      data: {
        userId: _that.data.userInfo.userId,
        activityId: this.data.activityId
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success(res) {
        var _d = res.data;
        if(_d.data) {
          _that.setData({
            isFavFlag: _d.data
          });
        }
      }
    });
    
  },











  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var _share = {
      title: this.data.info.title,
      imageUrl: this.data.info.coverImg,
      path: '/pages/activitys/detail/detail?id=' + this.data.activityId
    };
    return _share;
  }


})